
#include <stdint.h>
#include "main.h"
#include "AI.h"
#include "Thermistor.h"
#include "Adc.h"
#include "ADScan.h"
#include "Timer.h"

//#include "hal_adc1_dma.h"

#define PULL_RESISTOR    (47)
#define PULL_RESISTOR_47k    (470)
static  int16_t  AI_Value[8];
static  uint8_t  ActivateIndex;
static  uint32_t  AI_Service_Tick;
//Pull-up Res Unit : KOHM
static float  test[4];
int32_t Temp_GetResistor(uint16_t adc, int16_t pull_res)
{
  float ret_value;
  uint16_t adcx;

	adcx = adc + 65;
  ret_value= adcx;
	test[0] = ret_value;
  ret_value*= pull_res;
	test[1] = ret_value;
  ret_value/= (4095 - adcx);
	test[2] = ret_value;
	//ret_value/=10;
	//test[3] = ret_value;
  
  return (int32_t)ret_value; 
}
static u32 ResValue[2];

int32_t Temp_GetTempValue(uint8_t chn, NTC_TYPE_T type)
{
  int32_t temp;
  uint16_t adc;
  
  adc= Get_AD_Average(chn);
  
  if(adc > 4070)    // Open Circuit
    return (-9999);
  
  if (chn == ADC_CHN_T1)
  {
    temp= Temp_GetResistor(adc, PULL_RESISTOR_47k); 
		
		ResValue[0] = temp;
  }
  else
  {
     //temp= Temp_GetResistor(adc, PULL_RESISTOR);
		 temp= Temp_GetResistor(adc, PULL_RESISTOR_47k); 
		 temp*= 10;
		 ResValue[1] = temp;
  }
  if(temp < 2)    // Short Circuit
    return (-9999);
  
  temp= Therm_GetTemp(temp*10, type);
  
  return temp;
}

float ppp[7];
//Pressure Unit: bar
int32_t Press_GetGagePress(uint8_t chn)
{
  float fvol;
  uint16_t adcx;

	adcx = Get_AD_Average(chn) + 64;
	ppp[0] = adcx;
	fvol = adcx;
	ppp[1] = fvol;
  //fvol= Get_AD_Average(chn);
  fvol*= 2.5f;
	ppp[2] = fvol;
	adcx = Get_AD_Average(ADC_CHN_REF) + 64;
	ppp[3] = adcx;
	fvol/= adcx;
	ppp[4] = fvol;
 // fvol/= Get_AD_Average(ADC_CHN_REF);
  fvol= fvol*50.0/30.0;
	ppp[5] = fvol;
	//x = 30.0;
	//fvol/= x;
  
	
	
  if (fvol <= 0.25) {
    return (9999);
  } else if (fvol >= 4.75f) {
    return (-9999);
  }

  fvol -= 0.5f;
  fvol /= 4.0f;
  if (chn== AI_LO_PRESS)
  {
    fvol *= 20.0f;
  }
  else 
  {
    fvol *= 46.0f;
  }  
  if(fvol < 0.0f)
  {
    fvol= 0.0f;
  }
	fvol *= 100;
  ppp[6] = fvol;
  return (int32_t)fvol;
}



void AI_InputValue(uint8_t ch)
{
//  int32_t press;
  int32_t temp;
    

  switch(ch)
	{
		case 0:
					 //Analog Input
					temp= Temp_GetTempValue(ADC_CHN_T1, NTC_50K_B3970);//NTC_5K_B3324);
					if(temp > -9999)
					{
						temp*= 1;
					}
					AI_Value[ADC_CHN_T1] = temp;
					break;
					
		case 1:
					temp= Temp_GetTempValue(ADC_CHN_T2, NTC_15K_B3450);
					if(temp > -9999)
					{
						temp*= 1;
					}
					AI_Value[ADC_CHN_T2] = temp;			
					break;
					
		case 2:
					  temp= Temp_GetTempValue(ADC_CHN_T3, NTC_5K_A501440);//NTC_5K_B3324);
					if(temp > -9999)
					{
						temp*= 1;
					}
				 AI_Value[ADC_CHN_T3] = temp;			
					break;
		case 3:
					break;
		case 4:
					break;
		
		case 5:
					// ��ѹ--����ѹ��
					AI_Value[ADC_CHN_AI_1]= Press_GetGagePress(ADC_CHN_AI_1);
					break;
		
		case 6:
					// ��ѹ--����ѹ��
					AI_Value[ADC_CHN_AI_2]= Press_GetGagePress(ADC_CHN_AI_2);
					break;
		
		case 7:
					break;
		default:
					break;
	
	}
 
 
  
  //
 
  
  //

  
  
  
 
  
  
  
  //
 
}


u16	Get_AI_Average(	u8	channel	)
{
	
	if(channel >= AD_CHANNEL_AMOUNT)
	{
		return (u16)0;
	}
  
	
	return AI_Value[channel];
}


/*
 * Function Name:	AnalogIn_Trans_Service
 * Description:	ģ��������ת��������
 * Return Value:	u8	����8λ�޷�������0Ϊ��������0Ϊ���ִ���
 * Parameter:	��
 * Note:
 */
u8	AnalogIn_Trans_Service(	void	)
{

	if(Timer2_Get32Ticks() - AI_Service_Tick < 10)
	{
		return (u8)0;
	}
	AI_Service_Tick = Timer2_Get32Ticks();
         
    ActivateIndex++;
        
    if( ActivateIndex >=  AI_CHANNEL_AMOUNT)
    {
            
              ActivateIndex = 0;
    }
        
      
	AI_InputValue(ActivateIndex);
  //AI_InputValue(5);     
        



        
        
        

	return (u8)0;
}
